package main

import (
	"log"

	"gitee.com/uxff/beatdemo/beat"
)

func continuousWorks(o *beat.One, mateTeam, targetTeam map[int32]*beat.One) {
	for idb := range targetTeam {
		continuousWork(o, mateTeam, targetTeam[idb])
	}
}

func continuousWork(o *beat.One, mates map[int32]*beat.One, target *beat.One) {
	switch o.Role {
	case "A":
		continuousA(o, target)
	case "T":
		continuousA(o, target)
	case "H":
		sequenceH(o, mates, target)
	}
}

func continuousA(o *beat.One, target *beat.One) {
	// log.Printf("i(%s) perform A to %s", o.ToString(), target.ToString())
	for o.H > 0 && target.H > 0 {
		if o.Hit(target) == false {
			break
		}
	}
}
func continuousAByBot(o *beat.One, pmap map[int32]*beat.One) {
	// log.Printf("i(%s) perform A to %s", o.ToString(), target.ToString())
	randIdx := 0
	var aoeMultiple float32 = 1.0
	for id := range pmap {
		target := pmap[id]
		for o.H > 0 && target.H > 0 {
			randIdx++
			if o.Hit(target) == false {
				break
			}
			if randIdx%7 == 0 {
				o.Aoe(pmap, 0.33)
			} else if randIdx%31 == 0 {
				o.Aoe(pmap, 0.8*aoeMultiple)
				aoeMultiple *= 1.25
			}
		}
	}
}

func sequenceH(o *beat.One, mates map[int32]*beat.One, threatTarget *beat.One) {
	// log.Printf("i(%s) perform H threat %s", o.ToString(), threatTarget.ToString())
	//1 find out a target who need heal
	//2 heal it
	idList := make([]int32, len(mates))
	idx := 0
	for i := range mates {
		idList[idx] = i
		idx++
	}
	// some use asc, some use desc
	if o.Id%2 == 0 {
		for i := 0; i < len(idList)/2; i++ {
			idList[i], idList[len(idList)-1] = idList[len(idList)-1], idList[i]
		}
	}
	log.Printf("i(%s) perform H threat %s, mates list:%v", o.ToString(), threatTarget.ToString(), idList)

	for o.H > 0 && threatTarget.H > 0 {
		for _, i := range idList {
			if o.H > 0 && mates[i].H > 0 && mates[i].H < mates[i].MaxH {
				o.Heal(mates[i])
			}
		}
	}
}
